'use strict';
import {
    apiDomain
} from 'js/config.js';
var $ = require('jquery');
import {
    userSession
} from 'js/core/userSession';


let CallApi = function (uri, data, cb) {
    let APIURL = userSession.get().APIURL,
        apiurl;
    if (APIURL) {
        apiurl = APIURL.indexOf("http") > -1 ? APIURL : ("http://" + APIURL + "/");
    } else {
        apiurl = apiDomain;
    }

    data = data || {};
    let userkey = userSession.get().USERKEY;
    data.UserKey = userkey
    $.ajax({
        url: apiurl + uri,
        data: data,
        success: function () {
            var data;
            console.log(uri);
            try {
                data = JSON.parse(arguments[2].responseText);
            } catch (e) {
                var parser = new DOMParser();
                var xmlDoc = parser.parseFromString(arguments[2].responseText, "application/xml");
                data = JSON.parse(xmlDoc.childNodes[0].textContent);
            }
            console.log(data.result)
            if (data.message === 'error') {
                window.location.href = "#login";
                return
            } else {
                cb(data.result)
            }

        },
    });
};

export {
    CallApi
}